package com.example.petshopping.ui.bidding.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.petshopping.R;
import com.example.petshopping.ui.bidding.model.BiddingProduct;

import java.util.ArrayList;
import java.util.List;

public class BiddingProductAdapter extends RecyclerView.Adapter<BiddingProductAdapter.ProductViewHolder> {

    private Context context;
    private List<BiddingProduct> productList;
    private List<BiddingProduct> filteredList;
    private OnBidClickListener listener;
    
    private boolean showOnlyMyBids = false;
    private boolean showCompletedItems = false;
    private String currentTab = "all"; // 可以是"all"或"today"

    public BiddingProductAdapter(Context context, List<BiddingProduct> productList, OnBidClickListener listener) {
        this.context = context;
        this.productList = productList;
        this.listener = listener;
        this.filteredList = new ArrayList<>(productList);
        applyFilters();
    }

    public interface OnBidClickListener {
        void onBidClick(BiddingProduct product);
    }

    @NonNull
    @Override
    public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(context).inflate(R.layout.item_bidding_product, parent, false);
        return new ProductViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ProductViewHolder holder, int position) {
        BiddingProduct product = filteredList.get(position);
        
        // 设置商品图片
        holder.imgProduct.setImageResource(product.getImageResource());
        
        // 设置商品标题
        holder.tvTitle.setText(product.getTitle());
        
        // 设置商品价格
        holder.tvPrice.setText("¥" + product.getPrice());
        
        // 设置拍卖时间
        holder.tvAuctionTime.setText(product.getAuctionTime());
        
        // 设置竞拍状态
        String statusText = String.format("已出价%d次，%d人围观", product.getBidCount(), product.getViewerCount());
        holder.tvStatus.setText(statusText);
        
        // 设置"无理由"标签可见性
        holder.tvNoReason.setVisibility(product.isNoReason() ? View.VISIBLE : View.GONE);
        
        // 根据是否领先设置领先标签的可见性
        holder.tvLeading.setVisibility(product.isLeading() ? View.VISIBLE : View.GONE);
        
        // 根据是否已截拍设置按钮状态和样式
        if (product.isCompleted()) {
            holder.btnBid.setText("已截拍");
            holder.btnBid.setBackgroundResource(R.drawable.bg_button_gray);
            holder.itemView.setAlpha(0.6f);
            
            // 仅围观标签
            if (product.isWatchOnly()) {
                holder.imgCornerTag.setVisibility(View.VISIBLE);
            } else {
                holder.imgCornerTag.setVisibility(View.GONE);
            }
        } else {
            holder.btnBid.setText("出价");
            holder.btnBid.setBackgroundResource(R.drawable.bg_button_purple);
            holder.itemView.setAlpha(1.0f);
            holder.imgCornerTag.setVisibility(View.GONE);
        }
        
        // 设置出价按钮点击事件
        holder.btnBid.setOnClickListener(v -> {
            if (listener != null) {
                listener.onBidClick(product);
            }
        });
    }

    @Override
    public int getItemCount() {
        return filteredList.size();
    }
    
    public void filterByMyBids(boolean showOnlyMyBids) {
        this.showOnlyMyBids = showOnlyMyBids;
        applyFilters();
    }
    
    public void filterByTab(String tab) {
        this.currentTab = tab;
        applyFilters();
    }
    
    public void filterByCompletedStatus(boolean showCompletedItems) {
        this.showCompletedItems = showCompletedItems;
        applyFilters();
    }
    
    private void applyFilters() {
        filteredList.clear();
        
        for (BiddingProduct product : productList) {
            boolean passesMyBidFilter = !showOnlyMyBids || product.isMyBid();
            boolean passesTabFilter = currentTab.equals("all") || (currentTab.equals("today") && product.isTodayEnding());
            boolean passesCompletedFilter = !product.isCompleted() || showCompletedItems;
            
            if (passesMyBidFilter && passesTabFilter && passesCompletedFilter) {
                filteredList.add(product);
            }
        }
        
        notifyDataSetChanged();
    }

    static class ProductViewHolder extends RecyclerView.ViewHolder {
        ImageView imgProduct;
        TextView tvTitle;
        TextView tvPrice;
        TextView tvAuctionTime;
        TextView tvStatus;
        TextView tvNoReason;
        TextView tvLeading;
        Button btnBid;
        ImageView imgCornerTag;

        public ProductViewHolder(@NonNull View itemView) {
            super(itemView);
            imgProduct = itemView.findViewById(R.id.img_product);
            tvTitle = itemView.findViewById(R.id.tv_title);
            tvPrice = itemView.findViewById(R.id.tv_price);
            tvAuctionTime = itemView.findViewById(R.id.tv_auction_time);
            tvStatus = itemView.findViewById(R.id.tv_status);
            tvNoReason = itemView.findViewById(R.id.tv_no_reason);
            tvLeading = itemView.findViewById(R.id.tv_leading);
            btnBid = itemView.findViewById(R.id.btn_bid);
            imgCornerTag = itemView.findViewById(R.id.img_corner_tag);
        }
    }
} 